<?php

namespace app\web\controller;

use app\common\controller\Api;
use app\user\model\User;
use think\Db;
use Carbon\Carbon;
use think\Model;

/**
 * 前台首页控制器
 * @package app\index\controller
 */
class Work extends Auth
{
    public $noNeedLogin = [];

    /**
     * 获取员工打卡记录
     */
    public function clock_log(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['limit']) || empty($getData['limit'])){
                $getData['limit'] = '10';
            }
            $sql = Db::name('qt_work_log')
                ->alias('l')
                ->leftJoin('qt_users u','u.id = l.user_id')
                ->field('l.id,l.type,l.work_type,l.created_time,l.day,u.real_name,u.work_number,u.group')
                ->where('u.store_id',$this->userInfo['store_id']);
            if(isset($getData['start_time']) && !empty($getData['start_time']) && isset($getData['end_time']) && !empty($getData['end_time'])){
                $start_time = strtotime($getData['start_time']);
                $end_time = strtotime($getData['end_time']);
                $sql = $sql->whereBetweenTime('l.created_time',$start_time,$end_time);
            }
            //部门筛选
            if (isset($getData['group']) && !empty($getData['group']))
            {
                $sql = $sql->where('u.group',$getData['group']);
            }
            //人员筛选
            if (isset($getData['user_id']) && !empty($getData['user_id']))
            {
                $sql = $sql->where('u.user_id',$getData['user_id']);
            }
            $data = $sql->group('l.user_id')->paginate( $getData['limit']);
            $this->result('查询成功',$data,200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 获取考勤登记列表
     */
    public function attendance(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['limit']) || empty($getData['limit'])){
                $getData['limit'] = '10';
            }
            $sql = Db::name('qt_work_attendance')
                ->alias('a')
                ->leftJoin('qt_users u','u.id = a.user_id')
                ->field('a.id,a.group,a.engineer_id,a.reason,a.rank,a.start_time,a.end_time,a.handle_time,a.day,a.reason,a.rank,u.real_name,a.create_time,a.status')
                ->where('a.store_id',$this->userInfo['store_id']);
            //时间筛选
            if(isset($getData['time_type']) && !empty($getData['time_type'])){
                //开始时间
                if($getData['time_type'] == '1'){
                    if(isset($getData['start_time']) && !empty($getData['start_time']) && isset($getData['end_time']) && !empty($getData['end_time'])){
                        $start_time = strtotime($getData['start_time']);
                        $end_time = strtotime($getData['end_time']);
                        $sql = $sql->whereBetweenTime('a.create_time',$start_time,$end_time);
                    }
                }
                //结束时间
                if($getData['time_type'] == '2'){
                    //时间筛选
                    if(isset($getData['start_time']) && !empty($getData['start_time']) && isset($getData['end_time']) && !empty($getData['end_time'])){
                        $start_time = strtotime($getData['start_time']);
                        $end_time = strtotime($getData['end_time']);
                        $sql = $sql->whereBetweenTime('a.handle_time',$start_time,$end_time);
                    }
                }
            }
            //部门筛选
            if (isset($getData['group']) && !empty($getData['group']))
            {
                $sql = $sql->where('a.group',$getData['group']);
            }
            //请假人
            if (isset($getData['engineer_id']) && !empty($getData['engineer_id']))
            {
                $sql = $sql->where('a.engineer_id',$getData['engineer_id']);
            }

            //原因
            if (isset($getData['reason']) && !empty($getData['reason']))
            {
                $sql = $sql->where('a.reason',$getData['reason']);
            }


            //登记人
            if (isset($getData['user_id']) && !empty($getData['user_id']))
            {
                $sql = $sql->where('a.user_id',$getData['user_id']);
            }

            //状态
            if (isset($getData['status']) && !empty($getData['status']))
            {
                $sql = $sql->where('a.status',$getData['status']);
            }
            $data = $sql->order('a.id desc')
                ->paginate($getData['limit'])->each(function ($item){
                $engineerRes = Db::name('qt_users')->where('id',$item['engineer_id'])->find();
                $item['work_number'] = $engineerRes['work_number'];
                $item['engineer_name'] = $engineerRes['real_name'];
                return $item;
            });
            $this->result('请求成功', $data, 200);
        }
    }



    /**
     * 添加考勤登记
     */
    public function attendance_add(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $validate = new \app\web\validate\Attendance();
            if (!$validate->scene('add')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['start_time'] = strtotime($getData['start_time']);
            $getData['end_time'] = strtotime($getData['end_time']);
            $getData['store_id'] = $this->userInfo['store_id'];
            $getData['user_id'] = $this->userInfo['id'];
            $getData['status'] = '1';
            $getData['create_time'] = time();
            try{
                Db::startTrans();
                //插入请假数据
                Db::name('qt_work_attendance')->insert($getData);
                //更新用户请假
                Db::name('qt_users')->where('id',$getData['engineer_id'])->update(['work_status'=>'7']);
                Db::commit();
            }catch (Exception $exception){
                Db::rollback();
                $this->result('添加失败','',204);
            }
            $this->result('添加成功','',200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 修改考勤登记
     */
    public function attendance_edit(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数','',204);
            }
            $find = Db::name('qt_work_attendance')->where('id',$getData['id'])->find();
            if(!empty($find)){
                $userRes = Db::name('qt_users')->where('id',$find['user_id'])->find();
                $engineerRes = Db::name('qt_users')->where('id',$find['engineer_id'])->find();
                $find['real_name'] = $userRes['real_name'];
                $find['work_number'] = $engineerRes['work_number'];
                $find['engineer_name'] = $engineerRes['real_name'];
            }
            $this->result('查询成功', $find, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 修改考勤登记
     */
    public function attendance_save(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $validate = new \app\web\validate\Attendance();
            if (!$validate->scene('edit')->check($getData)) {
                $this->result($validate->getError(), '', 204);
            }
            $getData['start_time'] = strtotime($getData['start_time']);
            $getData['end_time'] = strtotime($getData['end_time']);
            $getData['store_id'] = $this->userInfo['store_id'];
            $getData['user_id'] = $this->userInfo['id'];
            $getData['status'] = '1';
            unset($getData['create_time']);
            $inset = Db::name('qt_work_attendance')->where('id',$getData['id'])->update($getData);
            if($inset){
                $this->result('修改成功','',200);
            }
            $this->result('修改失败','',204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 销假
     */
    public function attendance_destroy(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['id']) || empty($getData['status'])){
                $this->result('缺少参数', '', 204);
            }
            $data = Db::name('qt_work_attendance')->where('id',$getData['id'])->find();
            try{
                Db::startTrans();
                //销假
                Db::name('qt_work_attendance')->where('id',$getData['id'])->update(['status'=>$getData['status'],'handle_time'=>time()]);
                //更新用户状态
                if($getData['status'] == '2'){
                    Db::name('qt_users')->where('id',$data['engineer_id'])->update(['work_status'=>'2']);
                }
                Db::commit();
            }catch (Exception $exception) {
                Db::rollback();
                $this->result('修改失败', '', 204);
            }
            $this->result('修改成功','',200);
        }
        $this->result('无效的请求', '', 204);
    }



    /**
     * 删除考勤登记
     */
    public function attendance_remove(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数','',204);
            }
            $del = Db::name('qt_work_attendance')->where('id',$getData['id'])->delete();
            if($del){
                $this->result('删除成功','',200);
            }
            $this->result('删除失败','',204);
        }
        $this->result('无效的请求', '', 204);
    }



    /**
     * 考勤报表
     */
    public function attendance_total(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $sql = Db::name('qt_work_attendance')
                ->alias('a')
                ->leftJoin('qt_users u','u.id = a.engineer_id')
                ->field('a.id,a.group,u.real_name,u.work_number,a.engineer_id')
                ->where('a.store_id',$this->userInfo['store_id']);
            $map=array();
            $maps=array();
            //时间筛选
            if(isset($getData['start_time']) && !empty($getData['start_time']) && isset($getData['end_time']) && !empty($getData['end_time'])){
                $start_time = strtotime($getData['start_time']);
                $end_time = strtotime($getData['end_time']);
                $map[]=['a.create_time','between time',[$start_time,$end_time]];
                $maps[]=['create_time','between time',[$start_time,$end_time]];
                //$sql = $sql->whereBetweenTime('a.create_time',$start_time,$end_time);
            }
//            //部门筛选
            if (isset($getData['group']) && !empty($getData['group']))
            {
                $map[]=['a.group','eq',$getData['group']];
                $maps[]=['group','eq',$getData['group']];
//                $sql = $sql->where('a.group',$getData['group']);
            }
            if(!isset($getData['limit']) || empty($getData['limit'])){
                $getData['limit'] = '10';
            }

            $engineerRes = $sql->where($map)->group('a.engineer_id')
                ->paginate($getData['limit'])
                ->each(function ($item) use( $maps) {
                        $item['baoting'] = Db::name('qt_work_attendance')->where($maps)->where('engineer_id',$item['engineer_id'])->where('reason','1')->sum('day');
                        $item['qingjia'] = Db::name('qt_work_attendance')->where($maps)->where('engineer_id',$item['engineer_id'])->where('reason','2')->sum('day');
                        $item['bingjia'] = Db::name('qt_work_attendance')->where($maps)->where('engineer_id',$item['engineer_id'])->where('reason','3')->sum('day');
                        $item['xiuxi'] = Db::name('qt_work_attendance')->where($maps)->where('engineer_id',$item['engineer_id'])->where('reason','4')->sum('day');
                        $item['xiujia'] = Db::name('qt_work_attendance')->where($maps)->where('engineer_id',$item['engineer_id'])->where('reason','5')->sum('day');
                        $item['kuanggong'] = Db::name('qt_work_attendance')->where($maps)->where('engineer_id',$item['engineer_id'])->where('reason','6')->sum('day');
                        $item['total'] = $item['baoting'] + $item['qingjia']+ $item['bingjia']+ $item['xiuxi']+ $item['xiujia']+ $item['kuanggong'];

                        return $item;
                });
            $this->result('请求成功', $engineerRes, 200);

        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 打卡记录汇总
     */
    public function clock_total(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            $sql = Db::name('qt_work_log')
                ->alias('l')
                ->leftJoin('qt_users u','u.id = l.user_id')
                ->field('l.id,u.group,u.real_name,u.work_number,l.user_id')
                ->where('l.store_id',$this->userInfo['store_id']);
            $map=array();
            $maps=array();
            if(isset($getData['start_time']) && !empty($getData['start_time']) && isset($getData['end_time']) && !empty($getData['end_time'])){
                $start_time = strtotime($getData['start_time']);
                $end_time = strtotime($getData['end_time']);
                $map[]=['l.created_time','between time',[$start_time,$end_time]];
                $maps[]=['log.created_time','between time',[$start_time,$end_time]];
                //$sql = $sql->whereBetweenTime('a.create_time',$start_time,$end_time);
            }
//            //部门筛选
            if (isset($getData['group']) && !empty($getData['group']))
            {
                $map[]=['u.group','eq',$getData['group']];
                $maps[]=['u.group','eq',$getData['group']];
//                $sql = $sql->where('a.group',$getData['group']);
            }
            //工号
            if (isset($getData['work_number']) && !empty($getData['work_number']))
            {
                $map[]=['u.work_number','eq',$getData['work_number']];
                $userId = Db::name('qt_users')->where('work_number',$getData['work_number'])->value('id');
                $maps[]=['log.user_id','eq',$userId];
            }
            if(!isset($getData['limit']) || empty($getData['limit'])){
                $getData['limit'] = '10';
            }
            $engineerRes = $sql->where($map)->group('l.user_id')
                ->paginate($getData['limit'])
                ->each(function ($item) use( $maps) {
                    $item['duration'] = Db::name('qt_work_log')
                        ->alias('log')
                        ->leftJoin('qt_users u','u.id = log.user_id')
                        ->where($maps)
                        ->where('log.user_id',$item['user_id'])
                        ->sum('log.duration');
                    return $item;
                });
            $this->result('请求成功', $engineerRes, 200);

        }
        $this->result('无效的请求', '', 204);
    }



    /**
     * 获取员工打卡记录
     */
    public function user_clock_log(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['limit']) || empty($getData['limit'])){
                $getData['limit'] = '10';
            }
            $sql = Db::name('qt_work_log')
                ->alias('l')
                ->leftJoin('qt_users u','u.id = l.user_id')
                ->field('l.id,l.type,l.work_type,l.created_time,u.real_name,u.work_number,u.group')
                ->where('u.store_id',$this->userInfo['store_id'])
                ->where('u.id',$getData['user_id']);
            $data = $sql->paginate( $getData['limit']);
            $this->result('查询成功',$data,200);
        }
        $this->result('无效的请求', '', 204);
    }



    /**
     * 获取无线网数据列表
     */
    public function wifiList(){
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $data = Db::name('qt_store_wifi')
                ->field('id,wifi,mac')
                ->where(['store_id'=>$userInfo['store_id']])
                ->select();
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 获取无线网数据列表
     */
    public function wifiAdd(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['wifi']) || empty($getData['wifi'])){
                $this->result('缺少参数', '', 204);
            }
            if(!isset($getData['mac']) || empty($getData['mac'])){
                $this->result('缺少参数', '', 204);
            }
            $getData['created_time'] = time();
            $getData['store_id'] = $this->userInfo['store_id'];
            $data = Db::name('qt_store_wifi')->insert($getData);
            if($data){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 获取无线网数据列表
     */
    public function wifiDel(){
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if(!isset($getData['id']) || empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $del = Db::name('qt_store_wifi')->where('id',$getData['id'])->delete();
            if($del){
                $this->result('删除成功', '', 200);
            }
            $this->result('删除失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


}
